package com.sdy.dataadapter.util;

public class FieldUtil {
    /**
     * 驼峰转下划线
     */
    public static String underscoreName(String camelCaseName, boolean upperCase) {
        StringBuilder result = new StringBuilder();
        if (camelCaseName != null && camelCaseName.length() > 0) {
            String cc = camelCaseName.substring(0, 1);
            result.append(upperCase ? cc.toUpperCase() : cc.toLowerCase());
            for (int i = 1; i < camelCaseName.length(); i++) {
                char ch = camelCaseName.charAt(i);
                if (Character.isUpperCase(ch)) {
                    result.append("_");
                    result.append(upperCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
                } else {
                    result.append(upperCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
                }
            }
        }
        return result.toString();
    }

    /**
     * 下划线转驼峰
     */
    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean convert = false;
            boolean first = true;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if (ch == '_') {
                    convert = true;
                } else {
                    if (convert && !first) {
                        result.append(Character.toUpperCase(ch));
                        convert = false;
                    } else {
                        result.append(ch);
                        first = false;
                    }
                }
            }
        }
        return result.toString();
    }
}
